//
//  Type1ViewController.m
//  口袋
//
//  Created by tiankun on 15/5/23.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "Type1ViewController.h"
#import "Type1Model.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "CommentViewController.h"
#import "DKNightVersion.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface Type1ViewController ()
@property (nonatomic, retain)Type1Model *model;
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) NSInteger height;
@end

@implementation Type1ViewController
- (void)dealloc
{
    [_scroll release];
    [_str release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       self.rect = CGRectMake(0, 0, 0, 0);
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    NSString *type1Str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.str];
   
    [SANetWorkingTask request:type1Str parmater:nil block:^(NSDictionary *result) {

        
        self.model =[[Type1Model alloc]init];
        [self.model setValuesForKeysWithDictionary:[result objectForKey:self.str]];
        NSString *note = [self filterHTML:self.model.body];
        //
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
        //计算文本大小
        self.rect = [note  boundingRectWithSize:CGSizeMake(WIDTH - (0.027 * WIDTH) * 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
        [self.view addSubview:self.scroll];
        [self.scroll release];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.027 * WIDTH, 0.05 * WIDTH, WIDTH - (0.027 * 2 * WIDTH), 0.08 * WIDTH)];
        titleLabel.font = [UIFont boldSystemFontOfSize:21];
        titleLabel.text = self.model.title;
        titleLabel.nightTextColor = [UIColor lightGrayColor];
        [self.scroll addSubview:titleLabel];
        [titleLabel release];
        
        UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.027 * WIDTH, 0.048* WIDTH + titleLabel.frame.size.height, WIDTH - (0.027 * 2 * WIDTH), 0.053 * WIDTH)];
        NSString *source = self.model.source;
        NSString *time = self.model.ptime;
        NSString *ptime = [NSString stringWithFormat:@"%@     %@", source,time];
        timeLabel.font = [UIFont systemFontOfSize:13];
        timeLabel.text = ptime;
        timeLabel.textColor = [UIColor grayColor];
        [self.scroll addSubview:timeLabel];
        [timeLabel release];
        
        //创建文本
        if (self.model.img.count) {
            self.height = 0.1 * HEIGHT + 64;
            for (int i = 0; i < self.model.img.count; i++) {
                NSDictionary *dic = [self.model.img objectAtIndex:i];
                NSString *a = [dic objectForKey:@"pixel"];
                NSArray *sizeArray = [a componentsSeparatedByString:@"*"];
                CGFloat size = ((NSInteger)[sizeArray lastObject] * 1.0 / (NSInteger)[sizeArray firstObject] * 1.0);
                
                if ([a length] != 0) {
                    
                    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.027 * WIDTH, self.height, WIDTH - (0.027 * 2 * WIDTH), size * WIDTH - (0.027 * 2 * WIDTH))];
                    [imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"src"]] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
                    self.height += imageView.frame.size.height + 10;
                    
                    [self.scroll addSubview:imageView];
                    [imageView release];
                }
            }
            UILabel *noteLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.027 * WIDTH, self.height, WIDTH - (0.027 * 2 * WIDTH), self.rect.size.height)];
            noteLabel.numberOfLines = 0;
            noteLabel.text = note;
            noteLabel.font = [UIFont systemFontOfSize:20];
            noteLabel.nightTextColor = [UIColor lightGrayColor];
            [self.scroll addSubview:noteLabel];
            [noteLabel release];
            
        }else{
            UILabel *noteLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.027 * WIDTH, 0.1 * HEIGHT, WIDTH - (0.027 * 2 * WIDTH), self.rect.size.height)];
            noteLabel.numberOfLines = 0;
            noteLabel.text = note;
            noteLabel.font = [UIFont systemFontOfSize:20];
            noteLabel.nightTextColor = [UIColor lightGrayColor];
            [self.scroll addSubview:noteLabel];
            [noteLabel release];
        }
          [self.scroll setContentSize:CGSizeMake(0, self.rect.size.height + 0.1 * HEIGHT + 49 + self.height)];
        
#pragma mark -
#pragma mark 跟帖部分
        NSNumber *replayCount = self.model.replyCount;
        NSString * count = [NSString stringWithFormat:@"%@跟帖", replayCount];
        UIBarButtonItem * comments = [[UIBarButtonItem alloc] initWithTitle:count style:UIBarButtonItemStylePlain target:self action:@selector(commentAct)];
        [self.navigationItem setRightBarButtonItem:comments animated:YES];
        [comments release];

    }];

}
- (void)commentAct
{
    CommentViewController *comment = [[CommentViewController alloc]init];
    comment.type = self.model.replyBoard;
    comment.docID = self.str;
    [self.navigationController pushViewController:comment animated:YES];
    [comment release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
}
#pragma mark -
#pragma mark 去掉HTML标签
- (NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = @"p";
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@"\n"];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

@end
